package web_k;

import pojo_k.MainUser;
import service_k.MainUserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/mainUserModifyServlet")
public class MainUserModifyServlet extends HttpServlet {
    private MainUserService service = new MainUserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mainuserNameBefore = request.getParameter("mainuserNameBefore");
        String mainuserNameAfter = request.getParameter("mainuserNameAfter");
        String mainuserPasswordAfter = request.getParameter("mainuserPasswordAfter");

        MainUser mainUser = service.selectByName(mainuserNameAfter);

        //进行判断
        if (mainUser == null){
            //该用户名可用
            service.modify(mainuserNameBefore,mainuserNameAfter,mainuserPasswordAfter);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("您已修改成功！");
        } else {
            //用户名已存在，提示用户重新输入
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("该用户名已存在，请重新选择新的用户名");
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
